﻿using System;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Win32;

namespace RuoTeng.Util
{

    //获取机器码
    static class MachineCode
    {



        /// <summary> 
        /// 获取cpu序列号 
        /// </summary> 
        /// <returns> string </returns> 
        static public string GetCpuInfo()
        {
            try
            {

                string cpuInfo = " ";
                ManagementClass cimobject = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = cimobject.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString() + mo.Properties["Name"].Value.ToString();
                }
                return cpuInfo.ToString();

            }
            catch(Exception e)
            {
                return e.ToString();
            }
        }


        /// <summary>
        /// 获取网卡硬件地址 
        /// </summary>
        /// <returns></returns>
        public static string GetMacAddress()
        {
            try
            {
                //获取网卡硬件地址 
                string mac = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        mac = mo["MacAddress"].ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return mac;
            }
            catch
            {
                return "unknow";
            }
        }


        /// <summary>
        /// 获取IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetIPAddress()
        {
            try
            {
                //获取IP地址 
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        //st=mo["IpAddress"].ToString(); 
                        System.Array ar;
                        ar = (System.Array)(mo.Properties["IpAddress"].Value);
                        st = ar.GetValue(0).ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
        }


        /// <summary>
        /// 系统类型
        /// </summary>
        /// <returns></returns>
        public static string GetSystemType()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = mo["SystemType"].ToString();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
        }


        /// <summary> 
        /// 获取硬盘编号 
        /// </summary> 
        /// <returns> string </returns> 
        static public string GetHDid()
        {
            try
            {
                string HDid = " ";
                ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                return HDid.ToString();
            }
            catch
            {
                return "";
            }
        }


        /// <summary> 
        /// 获取biosID 
        /// </summary> 
        /// <returns> string </returns> 
        static public string GetBIOS()
        {
            try
            {


                string biosid = " ";
                ManagementClass cimobject1 = new ManagementClass("Win32_BIOS");
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    biosid = (string)mo.Properties["SerialNumber"].Value;
                }
                return biosid.ToString();
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 获取bios名称
        /// </summary>
        /// <returns></returns>
        static public string GetBIOSName()
        {
            try
            {
                string biosid = " ";
                ManagementClass cimobject1 = new ManagementClass("Win32_BIOS");
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    biosid = (string)mo.Properties["Name"].Value;
                }
                return biosid.ToString();
            }
            catch
            {
                return "";
            }
        }


        /// <summary>
        /// bios发布日期
        /// </summary>
        /// <returns></returns>
        static public string GetBIOSReleaseDate()
        {
            try
            {
                string biosid = " ";
                ManagementClass cimobject1 = new ManagementClass("Win32_BIOS");
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    biosid = (string)mo.Properties["ReleaseDate"].Value;
                }
                return biosid.ToString();
            }
            catch
            {
                return "";
            }
        }






        /// <summary> 
        /// 获取主板序列号 
        /// </summary> 
        /// <returns> string </returns> 
        static public string Getbaseboder()
        {
            try
            {
                string baseboder = " ";
                ManagementClass cimobject1 = new ManagementClass("Win32_BaseBoard");
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    baseboder = (string)mo.Properties["SerialNumber"].Value;
                }
                return baseboder.ToString();
            }
            catch
            {
                return "";
            }
        }



        /// <summary>
        /// 获取主板的pruduct属性
        /// </summary>
        /// <returns></returns>

        static public string GetBaseBorderPruduct()
        {

            try
            {
                string baseboder = " ";
                ManagementClass cimobject1 = new ManagementClass("Win32_BaseBoard");
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    baseboder = (string)mo.Properties["Product"].Value;
                }
                return baseboder.ToString();

            }
            catch
            {
                return "";
            }
        }






        /// <summary>
        /// 获取所有相关信息列表[为了调试]
        /// </summary>
        /// <returns></returns>
        static public string GetBoisList()
        {
            string bios = "";
            ManagementClass cimobject1 = new ManagementClass("Win32_BIOS");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();

            foreach (ManagementObject mo in moc1)
            {
                bios += "\r\n============BIOS信息＝＝＝＝＝＝＝＝＝＝＝";
                foreach (PropertyData pd in mo.Properties)
                {
                    bios += "\r\n" + pd.Name + ":\t";
                    if (pd.Value != null)
                    {
                        bios += pd.Value.ToString();
                    }
                }
            }
            return bios;

        }



        static public string GetBaseList()
        {
            string baseboder = "";
            ManagementClass cimobject1 = new ManagementClass("Win32_BaseBoard");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();

            foreach (ManagementObject mo in moc1)
            {
                baseboder += "\r\n============主板信息＝＝＝＝＝＝＝＝＝＝＝";
                foreach (PropertyData pd in mo.Properties)
                {
                    baseboder += "\r\n" + pd.Name + ":\t";
                    if (pd.Value != null)
                    {
                        baseboder += pd.Value.ToString();
                    }
                }
            }
            return baseboder;

        }



        static public string GetHDinfoList()
        {
            string hdinfo = "";
            ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();

            foreach (ManagementObject mo in moc1)
            {
                hdinfo += "\r\n============硬盘信息＝＝＝＝＝＝＝＝＝＝＝";
                foreach (PropertyData pd in mo.Properties)
                {
                    hdinfo += "\r\n" + pd.Name + ":\t";
                    if (pd.Value != null)
                    {
                        hdinfo += pd.Value.ToString();
                    }
                }
            }
            return hdinfo;

        }



        static public string GetcpuList()
        {
            string cpuinfo = "";
            ManagementClass cimobject1 = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();

            foreach (ManagementObject mo in moc1)
            {
                cpuinfo += "\r\n============cpu信息＝＝＝＝＝＝＝＝＝＝＝";
                foreach (PropertyData pd in mo.Properties)
                {
                    cpuinfo += "\r\n" + pd.Name + ":\t";
                    if (pd.Value != null)
                    {
                        cpuinfo += pd.Value.ToString();
                    }
                }
            }
            return cpuinfo;

        }




        /// <summary>
        /// 一代机器码
        /// </summary>
        /// <returns></returns>
        static public string GetMachinecode()
        {
            string mc = "";
            string pwd = "";
            MD5 md5 = MD5.Create();
            mc = GetCpuInfo() + Getbaseboder() + GetBIOS();
            if (mc.Trim() == "")
            {
                mc = "请开启服务Windows Management Instrumentation";
                return mc;
            }
            else
            {
                // 加密后是一个字节类型的数组
                byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(mc));
                // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
                for (int i = 0; i < s.Length; i++)
                {
                    // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符
                    pwd = pwd + s[i].ToString("X");

                    //加密之后进行封装
                    if (i % 4 == 0)
                    {
                        pwd += "-";
                    }
                }
                return pwd;
            }

        }


        /// <summary>
        /// 二代机器码
        /// </summary>
        /// <returns></returns>
        static public string GetNewMachinecode()
        {
            string mc = "";
            string pwd = "";
            MD5 md5 = MD5.Create();
            mc = GetCpuInfo() + Getbaseboder() + GetBIOS() + GetBaseBorderPruduct() + GetBIOSName() + GetBIOSReleaseDate();
            if (mc.Trim() == "")
            {
                mc = "请开启服务Windows Management Instrumentation";
                return mc;
            }
            else
            {
                // 加密后是一个字节类型的数组
                byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(mc));
                // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
                for (int i = 0; i < s.Length; i++)
                {
                    // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符
                    pwd = pwd + s[i].ToString("X");

                    //加密之后进行封装
                    if (i % 4 == 0)
                    {
                        pwd += "-";
                    }
                }
                return pwd;
            }

        }



    }
}
